package com.econ.powercloud.ems.common.enums;

import org.apache.commons.lang3.StringUtils;

/**
 * mqtt 发送方类型
 *
 * @author chenxiaoqi
 * @since 2021/10/12
 */
public enum DeviceOperationMsgType implements EnumMethod {

    YK(1, "yk", null, "遥控"),
    YT(2, "yt", null, "遥调"),
    DEMAND_CONTOL(3, "strategy", "1", "需量控制"),
    BATTERY_RESERVED(4, "strategy", "2","后备电源"),
    REVERSE_POWER(5, "strategy", "3","反功率倒送"),
    PEAK_VALLEY_BALANCE(6, "strategy", "4", "移峰填谷"),
    CLOUDSWITCH(7, "cloudSwitch", null, "云端控制"),
    CALL_HIS_PG(8, "callHisPg", null, "召唤历史保护事件"),
    CALL_HIS_SOE(9, "callHisSoe", null, "召唤历史SOE事件"),
    SOC_CONTROL(10,"strategy","5","SOC控制"),
    ;

    private final Integer code;
    private final String serviceId;
    private final String serviceCode;
    private final String desc;

    DeviceOperationMsgType(Integer code, String serviceId, String serviceCode, String desc) {
        this.code = code;
        this.serviceId = serviceId;
        this.serviceCode = serviceCode;
        this.desc = desc;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public String getServiceId() {
        return serviceId;
    }

    public String getServiceCode() {
        return serviceCode;
    }

    public static DeviceOperationMsgType fromCode(int code){
        for(DeviceOperationMsgType types: DeviceOperationMsgType.values()){
            if(types.getCode() == code){
                return types;
            }
        }
        return DeviceOperationMsgType.YK;
    }

    public static DeviceOperationMsgType from(String serviceId, String serviceCode){
        for(DeviceOperationMsgType type: DeviceOperationMsgType.values()){
            if (StringUtils.equals(serviceId, YK.serviceId)) {
                return YK;
            } else if (StringUtils.equals(serviceId, YT.serviceId)) {
                return YT;
            } else if (StringUtils.equals(serviceId, CALL_HIS_PG.serviceId)) {
                return CALL_HIS_PG;
            } else if (StringUtils.equals(serviceId, CALL_HIS_SOE.serviceId)) {
                return CALL_HIS_SOE;
            } else if(StringUtils.equals(serviceId, type.getServiceId()) && StringUtils.equals(serviceCode, type.getServiceCode())){
                return type;
            }
        }
        return DeviceOperationMsgType.YK;
    }
}
